package com.lhh.company.website.service;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhh.company.website.config.SysUserDetails;
import com.lhh.company.website.entity.IndexPic;
import com.lhh.company.website.entity.SysFile;
import com.lhh.company.website.mapper.IndexPicMapper;
import com.lhh.company.website.utils.SysUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* @author lx
* @description 针对表【index_pic(主页图片)】的数据库操作Service实现
* @createDate 2025-07-23 15:50:31
*/
@Service
public class IndexPicService extends ServiceImpl<IndexPicMapper, IndexPic> {
    @Value("${filepath}")
    String filepath;

    @Resource
    SysFileService sysFileService;

    @Transactional(rollbackFor = RuntimeException.class)
    public boolean save(IndexPic entity, SysUserDetails sysUserDetails) throws IOException {
        String tmpFilePath = filepath+"/tmp/"+entity.getFileId()+"."+entity.getFileType();
        String filePath = filepath+entity.getFileId()+"."+entity.getFileType();
        entity.setId(SysUtils.getId());
        entity.setCreatorid(sysUserDetails.getId());
        entity.setCreatorname(sysUserDetails.getUsername());
        Files.move(Paths.get(tmpFilePath),Paths.get(filePath));
        SysFile sysFile = new SysFile();
        sysFile.setId(entity.getFileId());
        sysFile.setFileType(entity.getFileType());
        sysFile.setFileUrl("/static/"+entity.getFileId()+"."+entity.getFileType());
        sysFile.setName(entity.getFileName());
        sysFile.setCreatorid(sysUserDetails.getId());
        sysFile.setCreatorname(sysUserDetails.getUsername());
        sysFileService.save(sysFile);
        entity.setFloatMsg(JSONUtils.toJSONString(entity.getFloatMsgList()));
        return super.save(entity);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    public boolean update(IndexPic entity, SysUserDetails sysUserDetails) throws IOException {
        //获取历史数据
        IndexPic hisentity = this.getById(entity.getId());
        entity.setCreatorid(sysUserDetails.getId());
        entity.setCreatorname(sysUserDetails.getUsername());
        if (!hisentity.getFileId().equals(entity.getFileId())) {
            //图片做了变更删除原有图片
            sysFileService.delete(hisentity.getFileId());
            SysFile sysFile = new SysFile();
            sysFile.setId(entity.getFileId());
            sysFile.setFileType(entity.getFileType());
            sysFile.setFileUrl("/static/"+entity.getFileId()+"."+entity.getFileType());
            sysFile.setName(entity.getFileName());
            sysFile.setCreatorid(sysUserDetails.getId());
            sysFile.setCreatorname(sysUserDetails.getUsername());
            String tmpFilePath = filepath+"/tmp/"+entity.getFileId()+"."+entity.getFileType();
            String filePath = filepath+entity.getFileId()+"."+entity.getFileType();
            Files.move(Paths.get(tmpFilePath),Paths.get(filePath));
            sysFileService.save(sysFile);
        }

        entity.setFloatMsg(JSONUtils.toJSONString(entity.getFloatMsgList()));
        return super.updateById(entity);
    }

    /**
     * 删除
     * 1.数据库删除
     * 2.文件数据库删除
     *
     * @param id
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteByid(String id) throws IOException {
        IndexPic entity = super.getById(id);
        super.removeById(id);
        sysFileService.delete(entity.getFileId());
    }
}




